/**
 * Footer for eaiis application.
 * It makes inputs and other form's elements much nicer.
 */

/**
 * Set inputs and select to change color when active.
 */
var arr = Common.combineArrays(Common.combineArrays(document.getElementsByTagName("input"), document.getElementsByTagName("select")), document.getElementsByTagName("textarea")); 
for (var i = 0; i < arr.length; ++i) {
	var obj = arr[i];
	if (obj.type == "text" || obj.type == "password" || obj.type == "file" || obj.type == "textarea") {
		Common.addAttributeAndKeepOld(obj, "onfocus", "Common.inputActive(this);");
		Common.addAttributeAndKeepOld(obj, "onblur", "Common.inputInActive(this);");

		if (obj.className != "") {
			obj.className += " " + obj.type;
		} else {
			obj.className = obj.type;
		}
	} else if (obj.type == "submit" || obj.type == "button") {
		// Set input buttons (submit, button)
		Common.addAttributeAndKeepOld(obj, "onfocus", "this.style.borderTopWidth = '2px'; this.style.borderLeftWidth = '2px'; this.style.borderBottomWidth = '0px'; this.style.borderRightWidth = '0px';");
		Common.addAttributeAndKeepOld(obj, "onblur", "this.style.borderTopWidth = '1px'; this.style.borderLeftWidth = '1px'; this.style.borderBottomWidth = '1px'; this.style.borderRightWidth = '1px';");
		Common.addAttributeAndKeepOld(obj, "onmouseover", "this.style.fontWeight = 'bold';");
		Common.addAttributeAndKeepOld(obj, "onmouseout", "this.style.fontWeight = 'normal';");
	} else if (new String(obj.nodeName).toLowerCase() == "select" || new String(obj.nodeName).toLowerCase() == "textarea") {
		Common.addAttributeAndKeepOld(obj, "onmouseover", "Common.inputActive(this);");
		Common.addAttributeAndKeepOld(obj, "onblur", "Common.inputInActive(this);");
	}
}

/**
 * Gather all inputs with and specific id.
 * They will be used in ajax calls.
 */
var arrAll = document.getElementsByTagName("input");
var arrOfInputOrderListNodes = [];
var patt = new RegExp("^offer-[0-9][0-9]*$");
var patt2 = new RegExp("^user-[0-9][0-9]*$");
for (var i = 0; i < arrAll.length; ++i) {
	if (patt.test(arrAll[i].value) || patt2.test(arrAll[i].value)) {
		arrOfInputOrderListNodes.push(arrAll[i]);
	}
}

/**
 * This nasty thing enables the green behaviour of
 * selected offers (when deleting, when adding them to the interested in box)
 */
for (var i = 0; i < arrOfInputOrderListNodes.length; ++i) {
	arrOfInputOrderListNodes[i].setAttribute("onclick", 
			"if (this.checked == true) {" +
				"this.parentNode.parentNode.style.backgroundColor = '#a2e086'; " +
				"if (this.parentNode.parentNode.className == 'description') {" +
					"this.parentNode.parentNode.previousSibling.previousSibling.style.backgroundColor = '#a2e086'; " +
				"}" +
			"} else { " +
				"this.parentNode.parentNode.style.backgroundColor = ''; " +
				"if (this.parentNode.parentNode.className == 'description') {" +
					"this.parentNode.parentNode.previousSibling.previousSibling.style.backgroundColor = ''; " +
				"}" +
			"}");
	if (arrOfInputOrderListNodes[i].checked == true) {
		arrOfInputOrderListNodes[i].parentNode.parentNode.style.backgroundColor = '#a2e086';
		if (arrOfInputOrderListNodes[i].parentNode.parentNode.className == 'description') {
			var parent = arrOfInputOrderListNodes[i].parentNode.parentNode.parentNode;
			//console.log(parent)
			for (var j = 0; j < parent.children.length; ++j) {
				//alert(parent.children[j].style.backgroundColor);
				console.log(parent.children[j].className)
				if (parent.children[j].className == 'pic') {
					console.log(parent.children[j].className)
					parent.children[j].style.backgroundColor = '#a2e086'; // TODO change children back to childNodes??
				}
			}
		}
	}
}



/**
 * Enable select all input
 */
var obj = document.getElementById("offer-list-selectall");
if (typeof(obj) != "undefined" && obj != null) {
	obj.setAttribute("onclick", "" +
		"if (this.checked == true) {" +
			"for (var i = 0; i < arrOfInputOrderListNodes.length; ++i) {" +
				"if (arrOfInputOrderListNodes[i].checked == false) {" +
					"arrOfInputOrderListNodes[i].click();" +
				"}" +
			"}" +
		"} else { " +
			"for (var i = 0; i < arrOfInputOrderListNodes.length; ++i) {" +
				"if (arrOfInputOrderListNodes[i].checked == true) {" +
					"arrOfInputOrderListNodes[i].click();" +
				"}" +
			"}" +
		"}");
}

/**
 * Move error messages written by php from the bottom to the top
 */
Errormsg.moveErrormsgBoxesFromBottomToUp();
